import { text, integer } from 'drizzle-orm/sqlite-core/columns';
import { sqliteTable, index } from 'drizzle-orm/sqlite-core';

import { auditSchema } from './audit';
import { table as feeds } from './feeds';
import { table as drivers } from './drivers';

export const tableName = 'links';

export const definition = {
  id: text('id').primaryKey(),
  uid: text().notNull(),
  url: text().notNull(),
  title: text().notNull(),
  feedId: text().references(() => feeds.id).notNull(),
  driverId: text().references(() => drivers.id).notNull(),
  disabled: integer('disabled', { mode: 'boolean' }),
  markAsRead: integer('markAsRead', { mode: 'boolean' }),
  saved: integer('saved', { mode: 'boolean' }),
};

export const table = sqliteTable(
  tableName,
  {
    ...definition,
    ...auditSchema,
  },
  (table) => [
    index('feedIndex').on(table.feedId),
    index('linkDriverIndex').on(table.driverId),
  ]
);
